<template>
  <a-select
    :placeholder="placeholder"
    :disabled="disabled"
    :value="value"
    @change="handleChange"
    :mode="multiple ? 'multiple' : ''"
    :showSearch="true"
    :filterOption="filterOption"
    allowClear>
    <a-select-option v-for="(item, index) in productList" :key="index" :value="item.id">
      {{ item.productName }} ({{ item.productCode }})
    </a-select-option>
  </a-select>
</template>

<script>
  import { getAction } from '@/api/manage'

  export default {
    name: 'JSelectProduct',
    props: {
      placeholder: {
        type: String,
        default: '请选择商品',
        required: false
      },
      disabled: {
        type: Boolean,
        default: false,
        required: false
      },
      value: {
        type: [String, Array],
        required: false
      },
      multiple: {
        type: Boolean,
        default: false,
        required: false
      },
      productType: {
        type: String,
        required: false
      }
    },
    data() {
      return {
        productList: []
      }
    },
    watch: {
      productType: {
        immediate: true,
        handler(val) {
          this.initProductList()
        }
      }
    },
    created() {
      this.initProductList()
    },
    methods: {
      initProductList() {
        let params = {}
        if (this.productType) {
          params.productType = this.productType
        }
        getAction('/wms/product/listAll', params).then(res => {
          if (res.success) {
            this.productList = res.result || []
          }
        }).catch(error => {
          console.error('获取商品列表出错:', error)
          this.productList = []
        })
      },
      handleChange(value) {
        this.$emit('change', value)
        this.$emit('input', value)
      },
      filterOption(input, option) {
        return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      },
      setCurrentValue(value) {
        this.initProductList()
        this.$emit('change', value)
        this.$emit('input', value)
      }
    }
  }
</script> 